/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
$(document).ready(function(){
	layui.use('form', function(){
	var form = layui.form;
    $(".attrlist-index").on('click','#create-cate-attr',function(){
        var cate_id = $(this).attr('data-cateid');
        var cate_name = $(this).attr('data-catename');
        var grade = $(this).attr('data-grade');
        contentHtml = '<div class="p30 ">'+
                '<table class="layui-table">'+
                    '<tr><td>属性名称：</td><td><input type="text" id="attr_name" value="" class="layui-input"/></td></tr>'+
                    '<tr><td >是否必填：</td><td><select name="attr_ismust" id="attr_ismust"><option value="0">选填</option><option value="1">必填</option></select></td></tr>'+
                    '<tr><td >属性类型：</td><td><select name="attr_type" id="attr_type"><option value="0">输入框</option><option value="1">选择项</option><option value="2">多选项</option><option value="3">文本域</option><option value="4">地区选择</option></select></td></tr>'+
                    '<tr id="tr-optional" class="tr-optional"><td >备选值：</td><td><textarea class="layui-textarea" id="optional-textarea" name="optional" placeholder="每一行为一个备选值"></textarea></td></tr>'+
                    '<tr><td >排序值：</td><td><input type="text" id="attr_sort" value="" class="layui-input"/></td></tr>'+
                '</table>'+
                '</div>';
		
        $attr_create_view = layer.open({
            type: 1,
			title:'为 <b>'+(cate_name)+'</b> 创建属性',			
            content: contentHtml,
			area: ['660px', '540px'],
            maxmin: true,
            btn: ['提交', '取消'],
            btnAlign: 'c',
            success: function(layero, index){
            	form.render();
            },
			yes: function(index, layero){
			    var type = $("#attr_type option:selected").val();
			    var name = $("#attr_name").val();
			    var sort = $("#attr_sort").val();
			    var optional = $("#optional-textarea").val();
			    var is_must = $("#attr_ismust option:selected").val();
			    
			    $params = JSON.stringify({'cate_id':cate_id,'name':name,'type':type,'sort':sort,'optional':optional,'is_must':is_must});
			    $create_back = ajaxSynchroRequest("/admincategory/category/attrcreate",$params);
			    console.log($create_back);				
				if($create_back.success===1){
					layer.msg($create_back.msg);
					window.location.reload();
				}
			    else{
			        layer.msg($create_back.msg);
			    }
			  }
        });
        $("#attr_type").change(function(){
            var selected_type = $("#attr_type option:selected").val();
            if(selected_type == 1 || selected_type == 2){
                $("#tr-optional").show();
            }else{
                $("#tr-optional").hide();
            }
            console.log(selected_type);
        });

        // $("#create-attr-submit").on('click',function(){
        //     var type = $("#attr_type option:selected").val();
        //     var name = $("#attr_name").val();
        //     var sort = $("#attr_sort").val();
        //     var optional = $("#optional-textarea").val();
        //     var is_must = $("#attr_ismust option:selected").val();
            
        //     $params = JSON.stringify({'cate_id':cate_id,'name':name,'type':type,'sort':sort,'optional':optional,'is_must':is_must});
        //     $create_back = ajaxSynchroRequest("/admincategory/category/attrcreate",$params);
        //     console.log($create_back);
        //     if($create_back.success===1){
        //         window.location.reload();
        //     }
        //     else{
        //         layer.open({
        //             content: $create_back.msg,
        //             skin: 'msg',
        //             time: 3 //3秒后自动关闭
        //         });
        //     }
        // });
    });
    $(".attrlist-index").on('click','.update-attr',function(){
        var attr_id = $(this).attr('data-attrid');
        $params = JSON.stringify({'attr_id':attr_id});
        $init_back = ajaxSynchroRequest("/admincategory/category/attrupdate",$params);
        console.log($init_back);
        ;
        $init_back.data.is_must;
        
        contentHtml = '<div class="p30">'+
                '<table class="layui-table">'+
                    '<tr><td >属性名称：</td><td><input type="text" id="attr_name" value="'+$init_back.data.name+'" class="layui-input"/></td></tr>'+
                    '<tr><td >是否必填：</td><td><select name="attr_ismust" id="attr_ismust">'+
                    '<option value="0">选填</option>'+
                    '<option value="1">必填</option></select></td></tr>'+
                    '<tr><td >属性类型：</td><td><select name="attr_type" id="attr_type">'+
                    '<option value="0">输入框</option>'+
                    '<option value="1">选择项</option>'+
                    '<option value="2">多选项</option>'+
                    '<option value="3">文本域</option>'+
                    '<option value="4">地区选择</option></select></td></tr>'+
                    '<tr id="tr-optional" class="tr-optional"><td >备选值：</td><td><textarea class="layui-textarea" id="optional-textarea" name="optional" placeholder="每一行为一个备选值">'+$init_back.optional+'</textarea></td></tr>'+
                    '<tr><td >排序值：</td><td><input type="text" id="attr_sort" value="'+$init_back.data.sort+'" class="layui-input"/></td></tr>'+
                '</table>'+
                '</div>';

		$attr_update_view = layer.open({
		    type: 1,
			title:'修改 <b>'+($init_back.data.name)+'</b> 属性',			
		    content: contentHtml,
			area: ['660px', '540px'],
		    maxmin: true,
		    btn: ['提交', '取消'],
		    btnAlign: 'c',
		    success: function(layero, index){
		    	form.render();
		    },
			yes: function(index, layero){
			    var type = $("#attr_type option:selected").val();
			    var name = $("#attr_name").val();
			    var sort = $("#attr_sort").val();
			    var optional = $("#optional-textarea").val();
			    var is_must = $("#attr_ismust option:selected").val();
			    
			    $params = JSON.stringify({'attr_id':attr_id,'name':name,'type':type,'sort':sort,'optional':optional,'is_must':is_must});
			    $create_back = ajaxSynchroRequest("/admincategory/category/attrupdate",$params);
			    console.log($create_back);
				if($create_back.success===1){
					layer.msg($create_back.msg);
					window.location.reload();
				}
				else{
				    layer.msg($create_back.msg);
				}
			  }
		});
		
		
		if($init_back.data.type == 1 || $init_back.data.type == 2){
		        $("#tr-optional").show();
		    }else{
		        $("#tr-optional").hide();
		    }
				 
		$("#attr_type").change(function(){
			var selected_type = $("#attr_type option:selected").val();
			if(selected_type == 1 || selected_type == 2){
				$("#tr-optional").show();
			}else{
				$("#tr-optional").hide();
			}
			console.log(selected_type);
		});
        
        $("#attr_ismust option[value='"+$init_back.data.is_must+"']").attr("selected",'selected');
        $("#attr_type option[value='"+$init_back.data.type+"']").attr("selected",'selected');
        
        // $("#update-attr-submit").on('click',function(){
        //     var type = $("#attr_type option:selected").val();
        //     var name = $("#attr_name").val();
        //     var sort = $("#attr_sort").val();
        //     var optional = $("#optional-textarea").val();
        //     var is_must = $("#attr_ismust option:selected").val();
            
        //     $params = JSON.stringify({'attr_id':attr_id,'name':name,'type':type,'sort':sort,'optional':optional,'is_must':is_must});
        //     $create_back = ajaxSynchroRequest("/admincategory/category/attrupdate",$params);
        //     console.log($create_back);
        //     if($create_back.success===1){
        //         window.location.reload();
        //     }
        //     else{
        //         layer.open({
        //             content: $create_back.msg,
        //             skin: 'msg',
        //             time: 3 //3秒后自动关闭
        //         });
        //     }
        // });
    });
    
    // $(".delete-attr").on('click',function(){
    //     if(confirm("确定要删除该属性吗？")){
    //         var attr_id = $(this).attr('data-attrid');
    //         $params = JSON.stringify({'attr_id':attr_id});
    //         $init_back = ajaxSynchroRequest("/admincategory/category/attrdelete",$params);
    //         if($init_back.success == 1){
    //             window.location.reload();
    //         }else{
    //             layer.open({
    //                 content: $create_back.msg,
    //                 skin: 'msg',
    //                 time: 3 //3秒后自动关闭
    //             });
    //         }
    //     }
    // });
	
	$(".delete-attr").on('click',function(){
		var attr_id = $(this).attr('data-attrid');
		layer.confirm('确定要删除该属性吗', {btn: ['删除', '取消' ]}, function(index, layero){		    
			$params = JSON.stringify({'attr_id':attr_id});
		    $init_back = ajaxSynchroRequest("/admincategory/category/attrdelete",$params);
		    if($init_back.success == 1){
		        layer.msg($init_back.msg);
				layer.close(index);
		        window.location.reload();
		    }
			    else{
			        layer.msg($init_back.msg);
			    }
		});
	});
});
});